package cn.xpride.framework.task;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TaskRunnerContext {

    private static ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
    private static TaskBizJob job = new TaskBizJob();
    private static ScheduledFuture<?> scheduledFuture;

    public static void start() {
        log.info("开启任务");
        scheduledFuture = scheduledThreadPool.scheduleAtFixedRate(job::monitor, 0, 5, TimeUnit.MINUTES);
    }

    public static void stop() {
        log.info("结束任务");
        scheduledFuture.cancel(true);
    }

    public static void runOnce() {
        log.info("运行一次任务");
        job.monitor();
    }

}
